package com.xueersi.interactivevideoeditor.utils
{
	import mx.messaging.Channel;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.messaging.channels.SecureAMFChannel;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class RemoteHelper
	{
		
		private static var _remoteURL:String;
		
		public function RemoteHelper()
		{
		}
		
	
		public static function set remoteURL(value:String):void
		{
			_remoteURL = value;
		}

		public static function getOperation(functionName:String,resultFun:Function,source:String="teacher.TeacherService",gateway:String = null,faultFun:Function=null):AbstractOperation
		{
			var ro:RemoteObject = new RemoteObject();
			ro.destination = "my-AMF";
			ro.makeObjectsBindable = true;
			var channelSet:ChannelSet = new ChannelSet();
			var amfChannel:AMFChannel = new AMFChannel("my-AMF",_remoteURL);
//			var amfSecurityChannel:SecureAMFChannel = new SecureAMFChannel("my-secuityAMF","")
//			channelSet.addChannel(amfSecurityChannel);	
			channelSet.addChannel(amfChannel)
			
			ro.channelSet = channelSet;
			ro.source = source;
			var abstractionOperaction:AbstractOperation =ro.getOperation(functionName);
			var onResultEvent:Function= function(event:ResultEvent):void
			{
				if(resultFun!=null)
					resultFun(event);	
			}	
			abstractionOperaction.addEventListener(FaultEvent.FAULT,onFaultEvent);
			abstractionOperaction.addEventListener(ResultEvent.RESULT,onResultEvent);
			return abstractionOperaction;
		}
		
		private static function onFaultEvent(event:FaultEvent):void
		{
			trace(event.fault.faultCode + "\n" + event.fault.faultString);
		}
	}
}